package org.apogeescience.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * DatabaseConfiguration loads the database.config file to setup information about the database
 * @author Isaac
 *
 */
public class DatabaseConfiguration {
	
	private static final String DATABASE_TYPE     = "database.type";
	private static final String DATABASE_ADDRESS  = "database.address";
	private static final String DATABASE_PORT     = "database.port";
	private static final String DATABASE_USERNAME = "database.userName";
	private static final String DATABASE_PASSWORD = "database.password";
	private static final String DATABASE_NAME     = "database.name";
	private static final String DATABASE_DRIVER   = "database.driver";
	
	private Properties props;

	/**
	 * Constructor for DatabaseConfiguration
	 * @throws IOException if database.config file can't be found
	 */
	public DatabaseConfiguration() throws IOException {
		this("database.config");
	}

	/**
	 * Constructor for DatabasConfiguration
	 * @param configFile configuration file for the database
	 * @throws IOException Throws the exception if the file cannot be loaded
	 */
	public DatabaseConfiguration(String configFile) throws IOException {
		setProps(new Properties());
		FileInputStream fis = new FileInputStream(configFile);
		getProps().load(fis);
	}
	
	/**
	 * Gets the database type
	 * @return
	 */
	public String getDatabaseType() {
		String typeStr = getProps().getProperty(DATABASE_TYPE);
		return typeStr.toUpperCase();
	}
	
	/**
	 * Gets the database ip address
	 * @return
	 */
	public String getDatabaseAddress() {
		return getProps().getProperty(DATABASE_ADDRESS);
	}
	
	/**
	 * Gets the port the database is listening on
	 * @return
	 */
	public int getDatabasePort() {
		String portStr = getProps().getProperty(DATABASE_PORT);
		return Integer.valueOf(portStr);
	}
	
	/**
	 * Gets the username to connect with 
	 * @return
	 */
	public String getDatabaseUserName() {
		return getProps().getProperty(DATABASE_USERNAME);
	}
	
	/**
	 * Gets the password for the database
	 * @return
	 */
	public String getDatabasePassword() {
		return getProps().getProperty(DATABASE_PASSWORD);
	}
	
	/**
	 * Gets the name of the database to connect too
	 * @return
	 */
	public String getDatabaseName() {
		return getProps().getProperty(DATABASE_NAME);
	}
	
	/**
	 * Gets the jdbc drivers to load
	 * @return
	 */
	public String getDatabaseDriver() {
		return getProps().getProperty(DATABASE_DRIVER);
	}

	/**
	 * Gets the properies
	 * @return
	 */
	private Properties getProps() {
		return this.props;
	}

	/**
	 * Sets the properites
	 * @param props
	 */
	private void setProps(Properties props) {
		this.props = props;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Connecting to Database\n");
		sb.append("Database Type: ").append(getDatabaseType()).append("\n");
		return sb.toString();
	}
}
